﻿using System;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using AccessControl.Business;
using AccessControl.SecurityMonitoring.Properties;
using DevExpress.XtraEditors;

namespace AccessControl.SecurityMonitoring.Form
{
    public partial class WorkflowSelector : XtraForm
    {

        #region Fields

        private Utility.Grid.CheckMarkSelection _checkMarkSelection;

        #endregion

        #region Constructor

        public WorkflowSelector()
        {
            InitializeComponent();

            var dt = GroupTypeBusiness.SelectAll();
            var row = dt.NewtblGroupTypeRow();
            row.PkGroupType = 0;
            row.GroupTypeName = @"هیچ کدام";
            dt.Rows.InsertAt(row, 0);
            lookUpEditSelectiveColumn.Properties.DataSource = dt;
            if (Settings.Default.NotifyGroupType != null)
            {
                lookUpEditSelectiveColumn.EditValue = Settings.Default.NotifyGroupType;
                //lookUpEditSelectiveColumn.Text =
                //dt.Where(row1 => row1.PkGroupType ==(int) ).Single().GroupTypeName;
            }
        }

        #endregion

        #region Method

        private void GetData()
        {
            var workflowDT = WorkflowBusiness.SelectAll();
            if (workflowDT != null)
                accessControlDS.tblWorkflow.Merge(workflowDT);
            accessControlDS.AcceptChanges();

        }

        private StringCollection GetSelectedWorkflow()
        {
            var array = new StringCollection();
            for (int i = _checkMarkSelection.selection.Count - 1; i >= 0; i--)
            {
                array.Add(((DataRowView)_checkMarkSelection.GetSelectedRow(i))["pkWorkflow"].ToString());
            }
            return array;
        }

        #endregion

        #region Properties

        private Boolean IsSelected
        {
            get
            {
                return gridViewWorkflow.SelectedRowsCount > 0;
            }
        }

        public StringCollection SelectedWorkflowList
        {
            get
            {
                return GetSelectedWorkflow();
            }
        }

        #endregion

        #region Event Handler

        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            if (IsSelected)
                simpleButtonOK.DialogResult = DialogResult.OK;
            Settings.Default.Save();
        }

        private void WorkflowSelector_Load(object sender, EventArgs e)
        {
            GetData();
            _checkMarkSelection = new Utility.Grid.CheckMarkSelection(gridViewWorkflow);
            _checkMarkSelection.SelectionChanged +=
                GridSelectionChanged;
        }

        private void WorkflowSelector_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (IsSelected)
                        simpleButtonOK.PerformClick();
                    break;

                case Keys.Escape:
                    if (IsSelected)
                        simpleButtonCancel.PerformClick();
                    break;
            }
        }

        private void gridViewWorkflow_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (IsSelected && (e.Clicks == 2))
                simpleButtonOK.PerformClick();
        }

        private void GridSelectionChanged(object sender, Utility.Grid.CheckMarkSelection.SelectionChangedEventArgs e)
        {
            simpleButtonOK.Enabled = _checkMarkSelection.SelectedCount > 0;
        }

        #endregion

    }
}